package com.zhiyou.app.shop.website.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.website.dao.IMoneyDealDao;
import com.zhiyou.app.shop.website.dao.pojo.MoneyDeal;
import com.zhiyou.app.shop.website.service.IMoneyDealService;

@Service(value="moneyDealService")
public class MoneyDealService implements IMoneyDealService {
	
	@Autowired
	private IMoneyDealDao moneyDealDao;

	public void findExchangeDeal(Page page, String userId, String subjectType, String isNull) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MoneyDeal.class);  
		 if (userId != null &&  !"".equals(userId)) {   
			 criteria.add(Expression.eq("userId", userId));  
		 }
		 if(subjectType != null && !"".equals(subjectType)){
			 criteria.add(Expression.eq("subjectType", subjectType));
		 }
		 if("null".equals(isNull)){
			 criteria.add(Expression.isNull("income"));
		 }
		 moneyDealDao.findByCriteria(page, criteria, Order.desc("createDate"));
	}

	public List findMoneyDeal(String userAccount, String subjectType,
			String startDate, String endDate, int start, int pageSize) {
		return moneyDealDao.findMoneyDeal(userAccount, subjectType, startDate, endDate, start, pageSize);
	}

	public int findMoneyDealCnt(String userAccount, String subjectType,
			String startDate, String endDate) {
		return moneyDealDao.findMoneyDealCnt(userAccount, subjectType, startDate, endDate);
	}


	public void findCharge(Page page, String account, String accountType, String accountSubject){
		DetachedCriteria criteria = DetachedCriteria.forClass(MoneyDeal.class);  
		 if (account != null && !"".equals(account)) {   
			 criteria.add(Expression.eq("remark", account));  
		 }
		 if(accountType != null && !"".equals(accountType)){
			 criteria.add(Expression.eq("accountType", accountType));
		 }
		 if(accountSubject != null && !"".equals(accountSubject)){
			 criteria.add(Expression.like("accountSubject", accountSubject, MatchMode.ANYWHERE));
		 }else{
			 criteria.add(Restrictions.or(Restrictions.like("accountSubject", "扣款",MatchMode.ANYWHERE),Restrictions.like("accountSubject", "充值",MatchMode.ANYWHERE)));
		 }
		 moneyDealDao.findByCriteria(page, criteria, Order.desc("createDate"));
	}

	public BigDecimal getStockExchang(String userId) {
		return moneyDealDao.getStockExchange(userId);
	}
}
